package com.gisocn.onemap.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.gisocn.onemap.base.Constant;
import com.gisocn.onemap.sgzx.R;
import com.google.gson.Gson;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

/* loaded from: classes2.dex */
public class MyView extends View {
    private static final float TOUCH_TOLERANCE = 4.0f;
    private int bitmapHeight;
    private int bitmapWidth;
    public boolean canClear;
    public ArrayList<DrawPath> deletePath;
    private DrawPath dp;
    private GetPointsListener getPointsListener;
    public int lineWidthNow;
    public int lineWidthOld;
    private Bitmap mBitmap;
    private Paint mBitmapPaint;
    public Canvas mCanvas;
    Paint mPaint;
    private Path mPath;
    private float mX;
    private float mY;
    private ArrayList<double[]> pointList;
    public ArrayList<DrawPath> savePath;

    /* loaded from: classes2.dex */
    public interface GetPointsListener {
        void getPoints(ArrayList<double[]> arrayList);
    }

    public MyView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.canClear = true;
        this.pointList = new ArrayList<>();
        this.getPointsListener = null;
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        this.bitmapWidth = displayMetrics.widthPixels;
        this.bitmapHeight = displayMetrics.heightPixels;
        initCanvas(this.lineWidthNow);
        this.savePath = new ArrayList<>();
        this.deletePath = new ArrayList<>();
    }

    public void initCanvas(int i) {
        Paint paint = new Paint();
        this.mPaint = paint;
        paint.setAntiAlias(true);
        this.mPaint.setDither(true);
        this.mPaint.setColor(getResources().getColor(R.color.colorRed));
        this.mPaint.setStyle(Paint.Style.STROKE);
        this.mPaint.setStrokeJoin(Paint.Join.ROUND);
        this.mPaint.setStrokeCap(Paint.Cap.ROUND);
        this.mPaint.setStrokeWidth(i);
        this.mBitmapPaint = new Paint(4);
        this.mBitmap = Bitmap.createBitmap(this.bitmapWidth, this.bitmapHeight, Bitmap.Config.ARGB_4444);
        this.mCanvas = new Canvas(this.mBitmap);
        this.mPath = new Path();
        this.mBitmapPaint = new Paint(4);
    }

    @Override // android.view.View
    protected void onDraw(Canvas canvas) {
        this.canClear = true;
        canvas.drawBitmap(this.mBitmap, 0.0f, 0.0f, this.mBitmapPaint);
        Path path = this.mPath;
        if (path != null) {
            canvas.drawPath(path, this.mPaint);
        }
    }

    /* loaded from: classes2.dex */
    public class DrawPath {
        int Progress;
        Paint paint;
        Path path;

        public DrawPath() {
        }
    }

    public void undo() {
        ArrayList<DrawPath> arrayList = this.savePath;
        if (arrayList == null || arrayList.size() <= 0) {
            return;
        }
        initCanvas(this.lineWidthNow);
        ArrayList<DrawPath> arrayList2 = this.savePath;
        this.deletePath.add(arrayList2.get(arrayList2.size() - 1));
        ArrayList<DrawPath> arrayList3 = this.savePath;
        arrayList3.remove(arrayList3.size() - 1);
        Iterator<DrawPath> it = this.savePath.iterator();
        while (it.hasNext()) {
            DrawPath next = it.next();
            this.mPaint.setStrokeWidth(next.Progress);
            next.paint.setStrokeWidth(next.Progress);
            this.mCanvas.drawPath(next.path, next.paint);
        }
        invalidate();
    }

    public void redo() {
        if (this.deletePath.size() > 0) {
            ArrayList<DrawPath> arrayList = this.deletePath;
            DrawPath drawPath = arrayList.get(arrayList.size() - 1);
            this.savePath.add(drawPath);
            Log.e("savePath: ", new Gson().toJson(this.deletePath));
            drawPath.paint.setStrokeWidth(drawPath.Progress);
            this.mCanvas.drawPath(drawPath.path, drawPath.paint);
            ArrayList<DrawPath> arrayList2 = this.deletePath;
            arrayList2.remove(arrayList2.size() - 1);
            invalidate();
        }
    }

    public void savedo(ArrayList<DrawPath> arrayList) {
        Log.e("paths:存在值-- ", new Gson().toJson(arrayList));
        if (arrayList.size() > 0) {
            initCanvas(arrayList.get(0).Progress);
            DrawPath drawPath = arrayList.get(arrayList.size() - 1);
            Log.e("dp.path", new Gson().toJson(drawPath.path));
            Log.e("dp.paint", new Gson().toJson(drawPath.paint));
            this.mCanvas.drawPath(drawPath.path, drawPath.paint);
            invalidate();
        }
    }

    public void removeAllPaint() {
        initCanvas(0);
        this.savePath.clear();
        this.deletePath.clear();
        invalidate();
    }

    public void close() {
        Bitmap bitmap = this.mBitmap;
        if (bitmap != null) {
            bitmap.recycle();
            this.mBitmap = null;
        }
    }

    public void setBitmap(String str) throws IOException {
        FileInputStream fileInputStream = null;
        try {
            try {
                FileInputStream fileInputStream2 = new FileInputStream(str);
                try {
                    Bitmap decodeStream = BitmapFactory.decodeStream(fileInputStream2);
                    this.mBitmap = decodeStream;
                    drawBitmapToCanvas(decodeStream);
                    fileInputStream2.close();
                } catch (FileNotFoundException e) {
                    e = e;
                    fileInputStream = fileInputStream2;
                    e.printStackTrace();
                    fileInputStream.close();
                    invalidate();
                } catch (Throwable th) {
                    th = th;
                    fileInputStream = fileInputStream2;
                    fileInputStream.close();
                    throw th;
                }
            } catch (FileNotFoundException e2) {
                e = e2;
            }
            invalidate();
        } catch (Throwable th2) {
            th = th2;
        }
    }

    public void drawBitmapToCanvas(Bitmap bitmap) {
        initCanvas(this.lineWidthNow);
        this.mBitmap.recycle();
        this.mBitmap = bitmap;
        this.mCanvas.drawBitmap(bitmap, 0.0f, 0.0f, this.mBitmapPaint);
        invalidate();
    }

    public String saveBitmap() {
        String str = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis())) + "paint.png";
        File file = new File(Constant.PAINT_PATH);
        File file2 = new File(Constant.PAINT_PATH, str);
        if (!file.exists()) {
            file.mkdir();
        } else if (file2.exists()) {
            file2.delete();
        }
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file2);
            this.mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
            return Constant.PAINT_PATH + str;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "";
        } catch (IOException e2) {
            e2.printStackTrace();
            return "";
        }
    }

    private void touch_start(float f, float f2) {
        this.mPath.reset();
        this.mPath.moveTo(f, f2);
        this.mX = f;
        this.mY = f2;
    }

    private void touch_move(float f, float f2) {
        float abs = Math.abs(f - this.mX);
        float abs2 = Math.abs(f2 - this.mY);
        if (abs >= TOUCH_TOLERANCE || abs2 >= TOUCH_TOLERANCE) {
            Path path = this.mPath;
            float f3 = this.mX;
            float f4 = this.mY;
            path.quadTo(f3, f4, (f + f3) / 2.0f, (f2 + f4) / 2.0f);
            this.mX = f;
            this.mY = f2;
        }
    }

    private void touch_up(int i) {
        this.mPath.lineTo(this.mX, this.mY);
        this.mCanvas.drawPath(this.mPath, this.mPaint);
        this.dp.Progress = i;
        this.savePath.add(this.dp);
        this.mPath = null;
    }

    @Override // android.view.View
    public boolean onTouchEvent(MotionEvent motionEvent) {
        float x = motionEvent.getX();
        float y = motionEvent.getY();
        double[] dArr = new double[2];
        int action = motionEvent.getAction();
        if (action == 0) {
            this.pointList.clear();
            this.mPath = new Path();
            DrawPath drawPath = new DrawPath();
            this.dp = drawPath;
            drawPath.path = this.mPath;
            this.dp.paint = this.mPaint;
            touch_start(x, y);
            invalidate();
            dArr[0] = x;
            dArr[1] = y;
            this.pointList.add(dArr);
        } else if (action == 1) {
            touch_up(this.lineWidthOld);
            invalidate();
            dArr[0] = x;
            dArr[1] = y;
            if (this.pointList.size() != 1) {
                this.pointList.add(dArr);
            }
            GetPointsListener getPointsListener = this.getPointsListener;
            if (getPointsListener != null) {
                getPointsListener.getPoints(this.pointList);
            }
        } else if (action == 2) {
            touch_move(x, y);
            invalidate();
            dArr[0] = x;
            dArr[1] = y;
            this.pointList.add(dArr);
        }
        return true;
    }

    public ArrayList<double[]> getPoints() {
        return this.pointList;
    }

    public void setGetPointsListener(GetPointsListener getPointsListener) {
        this.getPointsListener = getPointsListener;
    }
}
